﻿@{
    
    Layout = "~/Shared/Layouts/_Layout.cshtml";
    Page.Title = "Регистрација";
    
    var email = "";
    var password1 = "";
    var password2 = "";

    if(IsPost)
    {
        WebSecurity.Logout();
        
        email = Request["email"];
        password1 = Request["password1"];
        password2 = Request["password2"];
        
        // Validation
	    if (email.IsEmpty()) {
            ModelState.AddError("email", "Email адресата е потребна.");
        }
        
        if (!Validation.IsValid(email))
        {
            ModelState.AddError("email", "Неважечка Email адреса.");
        }

        if (password1.IsEmpty()) {
            ModelState.AddError("password1", "Лозинката е потребна.");
        }

        if(WebSecurity.UserExists(email))
        {
            ModelState.AddError("email", "Сметка со ова име веќе постои.");
        }

        if(password1 != password2)
        {
            ModelState.AddError("password1", "Лозинките не се совпаѓаат.");
        }
        
        // Create Account
        if(ModelState.IsValid)
        {
            WebSecurity.CreateUserAndAccount(email, password1, null, false);
            WebSecurity.Login(email, password1);
            if (Request.QueryString["sender"] != null)
            {
                Response.Redirect(Request.QueryString["sender"]);   
            }
            else
            {
                Response.Redirect("~/Default");
            }
        }  
    }

}

<h1>Регистрирајте нова сметка</h1>
<form action="Register?@Request.QueryString" method="post" class="accountForm">
    <p>
        @Html.Label("Email адреса: ", "email")<br />
        @Html.TextBox("email", email)
        @Html.ValidationMessage("email")
    </p>
    <p>
        @Html.Label("Лозинка: ", "password1")<br />
        @Html.Password("password1", password1)
        @Html.ValidationMessage("password1")
    </p>
    <p>
        @Html.Label("Потврди Лозинка: ", "password2")<br />
        @Html.Password("password2", password2)
    </p>
    <p>
         <input type="submit" value="Регистрација" />
    </p>
</form>
